'''
001 ROLL
Controls:       Rotate iPod
Description:    Roll the pig to the left or right
'''
import pygame
import spyral
import random  


class game001(spyral.scene.Scene):
    def __init__(self, player):
        spyral.scene.Scene.__init__(self)
        self.root_camera = spyral.director.get_camera()
        self.camera = self.root_camera.make_child(layers = [0,1,2,3])
        self.group = spyral.sprite.Group(self.camera)
        self.tilt_left = False;
        self.tilt_right = False;
        
        self.player = player;
        self.pig = pig();
        self.timer = Timer.Timer(self.player.current_speed);
        
        self.win = self.pickDirection(); #returns 'left' or 'right'
        if (self.win == 'left'):
            sign = Image.Image(Dic.images['001signleft'], (0, 251))
        else:
            sign = Image.Image(Dic.images['001signright'], (604, 244))
        sign.layer = 1;
        
        
        self.group.add(sign, self.pig, self.timer);
        
        
    def on_enter(self):
        bg = spyral.util.load_image(Dic.images['001bg'])
        self.camera.set_background(bg)
        

    def render(self):
        self.group.draw()
        self.root_camera.draw()
        
    def update(self, tick):
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_e:
                    self.pig.setRollLeft(True);
                elif event.key == pygame.K_q:
                    self.pig.setRollRight(True);
                if event.key == pygame.K_ESCAPE:
                    spyral.director.pop()
                    exit(0)
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_e:
                    self.pig.setRollLeft(False);
                if event.key == pygame.K_q:
                    self.pig.setRollRight(False);
        
        if (self.timer.timesUp()):
            if (self.winCondition() == False):
                self.player.decHealth();
            else:
                self.player.incCurrentGamesCompleted();
            spyral.director.pop()
        
        self.group.update();
    
    def winCondition(self):
        if (self.win == 'left'):
            if ( self.pig.rect.right <= 0 ):
                return True;
        else:
            if( Dic.geom['width'] <= self.pig.rect.left):
                return True;
        return False;
    
    def pickDirection(self):
        num = random.randint(1,2);
        if (num == 1):
            return 'left';
        else:
            return 'right';



class pig(spyral.sprite.Sprite):
    def __init__(self):
        spyral.sprite.Sprite.__init__(self)
        self.image = spyral.util.load_image(Dic.images['001roundpig'])
        self.original = self.image;
        self.pos = (291, 245)
        self.layer = 2;
        self.angle = 0;
        self.roll_left = False;
        self.roll_right = False;
        
    def update(self, tick):
        if (self.roll_left):
            #rotate CCW
            self.rotate(-5);
            #pos left
            self.rect.center = (self.rect.center[0] + 15, self.rect.center[1])
        elif (self.roll_right):
            #rotate CW
            self.rotate(5);
            #pos right
            self.rect.center = (self.rect.center[0] - 15, self.rect.center[1])
    
    
    def rotate(self, angle):
        self.angle += angle;
        oldcenter = self.rect.center;
        self.image = pygame.transform.rotate(self.original, self.angle);
        self.rect = self.image.get_rect();
        self.rect.center = oldcenter;
    
    def setRollLeft(self, bool):
        self.roll_left = bool;
    
    def setRollRight(self, bool):
        self.roll_right = bool;

        



if __name__ == "__main__":
    import sys
    sys.path.append("C:\Users\Patrick\paybackpigs\paybackpigs")
    from frenzy import Dic
    Dic.setImgFolder('../images/')
    from frenzy import Image
    from frenzy import Timer
    from frenzy import Player
    spyral.init()
    spyral.director.init(Dic.geom['size'], False, 'Payback Pigs', 30, 30)
    spyral.director.push(game001(Player.Player()))
    spyral.director.run()

else:
    import Image
    import Dic
    import Timer;